package com.yanqu.road.server.cmd.activity.unionwar;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.unionwar.UnionWarActivityServerProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.unionwar.UnionPhaseWarMgr;
import com.yanqu.road.server.manger.activity.unionwar.UnionWarActivityMgr;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;

@Cmd(code = Protocol.S_UNION_WAR_USER_SILVER_DONATE,desc = "备战-银两募捐")
public class UnionWarUserDonateSilverCmd extends AbstractCommand {

    private int activityId;
    private int phase;
    private int session;
    private String silverNum;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        UnionWarActivityServerProto.UserDonateSilverReqMsg reqMsg = UnionWarActivityServerProto.UserDonateSilverReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        phase = reqMsg.getPhase();
        session = reqMsg.getSession();
        if(reqMsg.hasSilverNum()){
            silverNum = reqMsg.getSilverNum();
        }
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(StringUtils.isNullOrEmpty(silverNum)){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
        }
        BigInteger silver = new BigInteger(silverNum);
        ActivityInfo activityInfo = UnionWarActivityMgr.getOpenActivityInfo(activityId);
        if(activityInfo == null){
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        String unionUid = GameUnionActivityGroupMgr.getUnionUid(activityId, player.getUserId());
        if(StringUtils.isNullOrEmpty(unionUid)){
            ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
            return;
        }
        ret = UnionPhaseWarMgr.donateSilver(player, activityId, unionUid, phase, session, silver);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            UnionWarActivityServerProto.UserDonateSilverRespMsg.Builder respMsg = UnionWarActivityServerProto.UserDonateSilverRespMsg.newBuilder();
            respMsg.setRet(ret);
            respMsg.setPhase(phase);
            respMsg.setSession(session);
            player.sendPacket(Protocol.U_UNION_WAR_USER_PATRONS_DISPATCH, respMsg);
        }
    }
}
